using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for DesignerLogControl.
	/// </summary>
	public class DesignerLogControl : AdminUserControl
	{
    private ExtendedDataGrid designerLogGrid;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private IContainer components;

		public DesignerLogControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the designer log", new ActionExecuteDelegate(RefreshLog));
      actionList.Add("Copy to clipboard", "Copy the designer log entries to the clipboard", 
        new ActionExecuteDelegate(CopyToClipboard));
      actionList.Add("Open selected entries", "Open the selected designer log entries", 
        new ActionExecuteDelegate(OpenSelected));
      actionList.Add("Delete selected entries", "Delete the selected designer log entries", 
        new ActionExecuteDelegate(DeleteSelected));
      actionList.Add("Delete old entries", "Delete any designer log entries over 30 days old", 
        new ActionExecuteDelegate(DeleteOld));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.designerLogGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.designerLogGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // designerLogGrid
      // 
      this.designerLogGrid.AllowNavigation = false;
      this.designerLogGrid.CaptionVisible = false;
      this.designerLogGrid.DataMember = "";
      this.designerLogGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.designerLogGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.designerLogGrid.Location = new System.Drawing.Point(0, 0);
      this.designerLogGrid.Name = "designerLogGrid";
      this.designerLogGrid.Size = new System.Drawing.Size(746, 352);
      this.designerLogGrid.TabIndex = 2;
      this.designerLogGrid.OpenSelected += new System.EventHandler(this.designerLogGrid_OpenSelected);
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(746, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 352);
      this.uiActionsControl.TabIndex = 4;
      // 
      // DesignerLogControl
      // 
      this.Controls.Add(this.designerLogGrid);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "DesignerLogControl";
      this.Size = new System.Drawing.Size(896, 352);
      ((System.ComponentModel.ISupportInitialize)(this.designerLogGrid)).EndInit();
      this.ResumeLayout(false);

    }
    #endregion

    private UIActionList actionList;
    public UIActionList ActionList
    {
      get
      {
        return actionList;
      }
    }

    public bool ActionsVisible
    {
      get
      {
        return uiActionsControl.Visible;
      }
      set
      {
        uiActionsControl.Visible = value;
      }
    }

    private string CopyToClipboard()
    {
      designerLogGrid.CopyToClipboard();
      return null;
    }

    private string OpenSelected()
    {
      using (new CursorHandler())
      {
        CurrencyManager bm = (CurrencyManager)designerLogGrid.BindingContext[designerLogGrid.DataSource];
        for (int i = 0; i < bm.Count; i++) 
        { 
          if(designerLogGrid.IsSelected(i))
          {
            DesignerLogEntry designerLogEntry = (DesignerLogEntry)bm.List[i];
            DesignerLogEntryForm form = new DesignerLogEntryForm();
            form.LoadDesignerLogEntry(designerLogEntry);
            form.Show();
          }
        }
      }
      return null;
    }

    public void LoadDesignerLog(DesignerLog log)
    {
//      IBindingList list = (IBindingList)log;
//      PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(log);
//      System.ComponentModel.PropertyDescriptor property = properties.Find("Time", false);
//      list.ApplySort(property, ListSortDirection.Descending);
      designerLogGrid.DataSource = null;
      designerLogGrid.DataSource = log;
      designerLogGrid.ClearColumns();
      designerLogGrid.AddColumn("Time");
      designerLogGrid.AddColumn("Description");
      designerLogGrid.AddColumn("ProcedureName", "Procedure");
      designerLogGrid.AddColumn("MapName", "Map");
      designerLogGrid.AddColumn("PreEvaluation", "Pre eval");
      designerLogGrid.AddColumn("PostEvaluation", "Post eval");
      designerLogGrid.AddColumn("StageName", "Stage");
      designerLogGrid.AddColumn("ActionName", "Action");
      designerLogGrid.AddColumn("FormName", "Form");
      designerLogGrid.AddColumn("FieldName", "Field");
      designerLogGrid.AddColumn("FolderId", "Folder ID");
      designerLogGrid.AddColumn("UserName", "User");
      designerLogGrid.AddColumn("Source");
      designerLogGrid.AddColumn("Property");
      designerLogGrid.AddColumn("DetectedByClass", "Detected by class");
      designerLogGrid.AddColumn("DetectedByMethod", "Detected by method");
      designerLogGrid.AutosizeColumns();
    }

    private string DeleteSelected()
    {
      if (MessageBox.Show("Delete these entries?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          designerLogGrid.DeleteSelected();
          OnRefreshRequired();
        }
      }
      return null;
    }

    private string DeleteOld()
    {
      using (new CursorHandler())
      {
        DesignerLog log = (DesignerLog)designerLogGrid.DataSource;
        int count = log.DeleteEntriesOlderThan(30);
        designerLogGrid.CurrentRowIndex = -1;
        designerLogGrid.RefreshGrid();
        OnRefreshRequired();
        return count + " entries deleted";
      }
    }

    private string RefreshLog()
    {
      DesignerLog log = (DesignerLog)designerLogGrid.DataSource;
      log.Refresh();
      designerLogGrid.RefreshGrid();
      OnRefreshRequired();
      return null;
    }

    private void designerLogGrid_OpenSelected(object sender, EventArgs e)
    {
      OpenSelected();
    }
  }
}
